<?php

declare(strict_types=1);

namespace app\eslib;

use think\facade\Config;

/**
 * 敏感词 助手类
 * @version 1.0.0
 */
class EsSensitiveWords
{
    private static $words = [];

    /**
     * 检测敏感词，返回内容中包含的敏感词的数组
     * @param string @text
     * @return array
     */
    public static function checkWords($text)
    {
        $outWords = [];
        if (!self::$words) {
            $words = Config::get('app_common.sensitive_words');
            if ($words) {
                self::$words = is_array($words) ? $words : array_filter(explode(',', $words));
            }
        }
        if (!self::$words || !$text) {
            return $outWords;
        }
        foreach (self::$words as $word) {
            if (false !== strpos($text, $word)) {
                $outWords[] = $word;
                continue;
            }
        }
        return $outWords;
    }
}
